package nccloud.web.pu.buyingreq.action;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import nc.bs.framework.common.NCLocator;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.pub.SqlBuilder;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.web.mmpub.pub.resexp.PfResumeExceptionNccUtils;

public class BuyingreqSupplierAfterEditAction implements ICommonAction {
	
	public Object doAction(IRequest request) {
		String read = request.read();
		JSONObject param = JSONObject.parseObject(read);
		String key = (String) param.get("key");
		JSONObject result = new JSONObject();
		if (key.equals("pk_supplier_v")) {
			try {
				JSONArray pk_supplier_vs = param.getJSONArray("value");
				String[] pks = new String[pk_supplier_vs.size()];
				if (pk_supplier_vs != null && pk_supplier_vs.size() > 0) {
					for (int i = 0; i < pks.length; i++) {
						pks[i] = pk_supplier_vs.get(i).toString();
					}
				}
				SqlBuilder sqlB = new SqlBuilder();
				sqlB.append(" select bd_supplier.pk_supplier pk_supplier,bd_supplier.name supplier_name,bd_supplier_v.pk_supplier_v,bd_supplier_v.name supplier_v_name, ");
				sqlB.append(" bd_supplier.def3,bd_supplier.def4,d1.name def4name,bd_supplier.def5,d2.name def5name,bd_supplier.def8,d3.name def8name from bd_supplier");
				sqlB.append(" inner join bd_supplier_v on bd_supplier.pk_supplier_v = bd_supplier_v.pk_supplier_v ");
				sqlB.append(" left join bd_defdoc d1 on bd_supplier.def4 = d1.pk_defdoc and d1.dr = 0 ");
				sqlB.append(" left join bd_defdoc d2 on bd_supplier.def5 = d2.pk_defdoc and d2.dr = 0 ");
				sqlB.append(" left join bd_defdoc d3 on bd_supplier.def8 = d3.pk_defdoc and d3.dr = 0 ");
				sqlB.append(" where bd_supplier.dr = 0 and bd_supplier_v.dr = 0 and bd_supplier.pk_supplier_v ", pks);
				List<Map<String, String>> list = (List<Map<String, String>>) NCLocator.getInstance().lookup(IUAPQueryBS.class)
						.executeQuery(sqlB.toString(), new MapListProcessor());
				if (list != null && list.size() > 0) {
					result.put("suppliervo",list);
				}
			} catch (BusinessException e) {
				return PfResumeExceptionNccUtils.handleResumeException(e);
			}
		}
		return result;
	}
}
